package com.gdth.listener.impl;

import android.content.Intent;
import android.support.v4.content.LocalBroadcastManager;

import com.gdth.listener.OnTelePhoneListener;
import com.gdth.service.PhoneCallState;

/**
 * 电话监听
 * @author arvin
 *
 */
public class OnTelePhoneListenerImpl implements OnTelePhoneListener{
	
	private LocalBroadcastManager mLocalBroadcastManager;
	
	public OnTelePhoneListenerImpl(LocalBroadcastManager mLocalBroadcastManager){
		this.mLocalBroadcastManager = mLocalBroadcastManager;
	}
	
	@Override
	public void CALL_STATE_IDLE() {
		// TODO Auto-generated method stub
		 
		Intent intent = new Intent(PhoneCallState.IDLE);
		intent.putExtra("msg", "电话空闲");
		sendBroadcastByIntent(intent);
	}

	@Override
	public void CALL_STATE_RINGING() {
		// TODO Auto-generated method stub
		sendBroadcastByIntent(PhoneCallState.ALERTING);
	}

	@Override
	public void CALL_STATE_OFFHOOK() {
		// TODO Auto-generated method stub
		sendBroadcastByIntent(PhoneCallState.ACTIVE);
	}
	
	@Override
	public void CALL_STATE_OFFHOOK_CALL() {
		// TODO Auto-generated method stub
		
		sendBroadcastByIntent(PhoneCallState.ACTIVE);
	}
	
	@Override
	public void CALL_STATE_OFFHOOK_DOWN(){
		sendBroadcastByIntent(PhoneCallState.DISCONNECTED);
	}
	
	/**
     * 发生本地广播
     * @param action
     */
    private void sendBroadcastByIntent(String action){
    	Intent intent = new Intent(action);
    	sendBroadcastByIntent(intent);
    }
    
    /**
     * 发生本地广播
     * @param intent
     */
    private void sendBroadcastByIntent(Intent intent){
    	mLocalBroadcastManager.sendBroadcast(intent);
    }
}
